<?xml version="1.0" encoding="UTF-8"?>
<!--
	Copyright 2009 Barnaby@iWare.co.uk
	
	Licensed under the Apache License, Version 2.0 (the "License"); you
	may not use this file except in compliance with the License. You may
	obtain a copy of the License at
	http://www.apache.org/licenses/LICENSE-2.0 Unless required by
	applicable law or agreed to in writing, software distributed under the
	License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
	CONDITIONS OF ANY KIND, either express or implied. See the License for
	the specific language governing permissions and limitations under the
	License.
-->
<xsl:stylesheet
	version="2.0"
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns="uri:simple-service.iware.co.uk"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>

	<xsl:output
		indent="yes"
	/>
	<!--
		Obviously parsing PDFs for the API isn't very reliable
		
		YOU are responsible for sanity checking the output.
		Absolutely no guarantee is made that the stub won't
		register 6 billion names the first time you login.
		
	 -->
	<xsl:variable name="font_api1" select="13" />
	<xsl:variable name="font_api2" select="-1" />
	<xsl:variable name="height_header" select="50" />
	<xsl:variable name="height_page" select="1050" />
	<xsl:variable name="left_param" select="142" />


	<xsl:template match="/">
		<xsl:processing-instruction name="xml-stylesheet">type="text/xsl" href="iware.simple-service.xsl"</xsl:processing-instruction>
		<iware package="com.enom.reseller" xsi:schemaLocation="uri:simple-service.iware.co.uk iware.simple-service.xsd">
			<xsl:comment>
				<xsl:apply-templates select="pdf2xml/page[1]/text" mode="comment"/>
			</xsl:comment>	
			<reuse xml:id="generic_out">
				<!-- Undocumented output params set by most operations. -->
				<param name="Language" />
				<param name="MinPeriod" />
				<param name="MaxPeriod" />
				<param name="Server" />
				<param name="Site" />
				<param name="IsLockable" />
				<param name="IsRealTimeTLD" />
				<param name="TimeDifference" />
				<param name="ExecTime" />
				<param name="ResponseCount" />
				<param name="errors" type="msg:errors" />
				<param name="responses" type="msg:responses" />
				<param name="debug" />
			</reuse>
			<type name="errors">
				<repeated>
					<param name="Err" />
				</repeated>
			</type>
			<type name="responses" sub="sequence">
				<param name="response" max="unbounded">
					<param name="ResponseNumber" />
					<param name="ResponseString" />
				</param>
			</type>
			<interface name="IEnom" >
				<xsl:variable name="orderedtext">
					<xsl:for-each select="/pdf2xml/page">
						<xsl:for-each select="text[@top &gt; $height_header and @top &lt; ($height_header+$height_page)]">
							<xsl:sort select="@top" data-type="number"/>
							<xsl:sort select="@left" data-type="number"/>
							<xsl:copy-of select="." />
						</xsl:for-each>
					</xsl:for-each>
				</xsl:variable>
				<xsl:for-each-group select="$orderedtext/text" group-starting-with="text[@font=$font_api1 or @font=$font_api2]">
					<xsl:if test="normalize-space(.)='AddToCart' ">
						<xsl:message>
							<xsl:text>NOT IMPLEMENTED: TODO: operations with multiple sets of params.</xsl:text>
							<xsl:value-of select="normalize-space(.)" />
						</xsl:message>
					</xsl:if>
					<xsl:if test="position() &gt; 1 and normalize-space(.)!='AddToCart'">
						<operation name="{normalize-space(.)}">
							<xsl:variable name="command" select="normalize-space(.)" />
							<xsl:for-each-group select="current-group()[not(starts-with(.,'Param name'))]" group-starting-with="text[b[normalize-space(.)='Input parameters']] | text[b[normalize-space(.)='Returned parameters and values']] | text[node()[1][local-name()='b' and normalize-space(.)='Notes']]">
								<xsl:if test="not(b[normalize-space(.)='Input parameters'] or b[normalize-space(.)='Returned parameters and values'] or b[normalize-space(.)='Notes'])">
									<xsl:comment>
										<xsl:apply-templates select="current-group()" mode="comment"/>
									</xsl:comment>
								</xsl:if>
								<xsl:if test="b[normalize-space(.)='Input parameters']">
									<in>
										<param name="ResponseType" default="XML" />
										<param name="Command" default="{$command}" />
										<xsl:variable name="left_required" select="following::text[@left=$left_param and node()[1][local-name()='b']][2]/following::text[1]/@left" />
										<xsl:variable name="left_comment" select="following::text[@left=$left_param and node()[1][local-name()='b']][2]/following::text[2]/@left" />
										<xsl:for-each-group select="current-group()" group-starting-with="text[@left=$left_param and node()[1][local-name()='b']]">
											<xsl:if test="contains(.,'*')">
												<xsl:message>
													<xsl:text>NOT IMPLEMENTED: Wildcard in param. Please update :</xsl:text>
													<xsl:value-of select="$command" />
												</xsl:message>
											</xsl:if>
											<xsl:if test="translate(substring(.,1,1),'123456789','000000000')='0'">
												<xsl:message>
													<xsl:text>NOT IMPLEMENTED: WSDL dosn't support params beginning with a digit.</xsl:text>
													<xsl:value-of select="$command" />
												</xsl:message>
											</xsl:if>
											
											<xsl:if test="position() &gt; 1 and normalize-space(.)!='ResponseType' and not(contains(.,'*')) and not(translate(substring(.,1,1),'123456789','000000000')='0') ">
												
												<xsl:if test="i[.='X']">
													<repeated>
															<param name="{substring-before(concat(normalize-space(b[1]),' '),' ')}">
																<xsl:comment>
																	<xsl:apply-templates select="current-group()[@left=$left_comment]" mode="comment"/>
																</xsl:comment> 
															</param>
													</repeated>
												</xsl:if>
												<!-- HACK: Including the repeated prams without numbers isn't always needed, but shouldn't ever matter. -->
												<param name="{substring-before(concat(normalize-space(b[1]),' '),' ')}">
													<xsl:if test="not(i[.='X']) and count(current-group()[@left=$left_required])=1 and current-group()[@left=$left_required]='Required'">
														<xsl:attribute name="min">1</xsl:attribute>
													</xsl:if>
													<xsl:comment>
														<xsl:apply-templates select="current-group()[@left=$left_comment]" mode="comment"/>
													</xsl:comment> 
													</param>
											</xsl:if>
										</xsl:for-each-group>
									</in>
								</xsl:if>
								<xsl:if test="b[normalize-space(.)='Returned parameters and values']">
									<out>
										<reuse ref="generic_out" />
										<xsl:variable name="left_comment" select="following::text[@left=$left_param and node()[1][local-name()='b']][2]/following::text[1]/@left" />
										
										<xsl:for-each-group select="current-group()" group-starting-with="text[@left=$left_param and node()[1][local-name()='b']]">
											<xsl:if test="contains(.,'*')">
												<xsl:message>
													<xsl:text>NOT IMPLEMENTED: Wildcard out param. Please update :</xsl:text>
													<xsl:value-of select="$command" />
												</xsl:message>
											</xsl:if>

											<xsl:if test="
	position() &gt; 1
and
	not(contains(.,'*'))
and
	normalize-space(.)!='Language'
and
	normalize-space(.)!='MinPeriod'
and
	normalize-space(.)!='MaxPeriod'
and
	normalize-space(.)!='Server'
and
	normalize-space(.)!='Site'
and
	normalize-space(.)!='IsLockable'
and
	normalize-space(.)!='IsRealTimeTLD'
and
	normalize-space(.)!='TimeDifference'
and
	normalize-space(.)!='ExecTime'
and
	normalize-space(.)!='ResponseCount'
and
	(not(starts-with(normalize-space(.),'Err'))
or
	contains(.,'ErrCount'))
											">
												<!-- Hack: Err elements seem to actually be nested in errors -->
												<xsl:if test="i[.='X']">
													<repeated>
															<param name="{substring-before(concat(normalize-space(b[1]),' '),' ')}">
																<xsl:comment>
																	<xsl:apply-templates select="current-group()[@left=$left_comment]" mode="comment"/>
																</xsl:comment> 
															</param>
													</repeated>
												</xsl:if>
												<param name="{substring-before(concat(normalize-space(b[1]),' '),' ')}">
													<xsl:comment>
														<xsl:apply-templates select="current-group()[@left=$left_comment]" mode="comment"/>
													</xsl:comment> 
												</param>
											</xsl:if>
										</xsl:for-each-group>
									</out>
								</xsl:if>
							</xsl:for-each-group>
						</operation>
					</xsl:if>
				</xsl:for-each-group>
			</interface>
			<service name="TestReseller" interface="IEnom" href="https://resellertest.enom.com/interface.asp">
				<xsl:comment>
					Enom's testing service.
				</xsl:comment>
			</service>
			<service name="Reseller" interface="IEnom" href="https://reseller.enom.com/interface.asp">
				<xsl:comment>
					Enom's live service (please be careful).
				</xsl:comment>
			</service>
		</iware>
	</xsl:template>
	
	<xsl:template match="text">
		<text>
			<xsl:apply-templates />
		</text>
	</xsl:template>

	<xsl:template match="text" mode="comment">
		<xsl:apply-templates mode="comment" />
			<xsl:text>
</xsl:text>
	</xsl:template>
	
	<xsl:template match="b" mode="comment">
		<xsl:text>*</xsl:text>
		<xsl:apply-templates mode="comment" />
		<xsl:text>*</xsl:text>
	</xsl:template>
	
	<xsl:template match="a" mode="comment">
		<xsl:text>_</xsl:text>
		<xsl:apply-templates mode="comment" />
		<xsl:text>_</xsl:text>
	</xsl:template>
</xsl:stylesheet>

